﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace SplendidCRM.Ext
{
    public class IframeWrappingAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var request = filterContext.RequestContext.HttpContext.Request;
            var isWrapped = request["__wrapped__"] == "1";
            if (isWrapped || request.Url == null) return;
            var parameters = request.QueryString.Render();
            var url = new UriBuilder(request.Url)
                          {
                              Query = parameters + (parameters.Length == 0 ? String.Empty : "&") + "__wrapped__=1"
                          };
            filterContext.Result = new RedirectResult("~/mvc/default.aspx?url=" + HttpUtility.UrlEncode(url.ToString()));
        }
    }
}
